package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.AreaBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.bean.SiteBean;
import com.emip.bean.ValleyBean;
import com.emip.dao.SiteDAO;
import com.emip.service.SiteService;

@Service("siteService")
public class SiteServiceImpl implements SiteService {

	@Autowired
	SiteDAO siteDAO;
	
	public SiteBean addSite(String id, MonitorTypeBean monitorType, AreaBean area,
			String name, String gprsID, ValleyBean valley, String qualityName, int qualityLevel,
			String contact, String telephone, String mobile, String eMail, String ipAddress,
			String port, int freqMin, String gpsFixed, String gpsLongitude, String gpsLatitude,
			String remark, float longitudeSetoff, float latitudeSetoff) {
		SiteBean newBean = new SiteBean();
		newBean.setId(id);
		newBean.setMonitorType(monitorType);
		newBean.setArea(area);
		newBean.setName(name);
		newBean.setGprsID(gprsID);
		newBean.setValley(valley);	
		newBean.setQualityName(qualityName);
		newBean.setQualityLevel(qualityLevel);
		newBean.setContact(contact);
		newBean.setTelephone(telephone);
		newBean.setMobile(mobile);
		newBean.seteMail(eMail);
		newBean.setIpAddress(ipAddress);
		newBean.setPort(port);
		newBean.setFreqMin(freqMin);
		newBean.setGpsFixed(gpsFixed);
		newBean.setFixtedLongitude(gpsLongitude);
		newBean.setFixtedLatitude(gpsLatitude);
		newBean.setRemark(remark);
		newBean.setLongitudeSetoff(longitudeSetoff);
		newBean.setLatitudeSetoff(latitudeSetoff);
		//查重，是否已经有相同的name的记录存在
		//若有重复记录，则返回null
		if (siteDAO.findExist(newBean)) {
			return null;
		}
		siteDAO.add(newBean);
		return newBean;
	}

	public Boolean deleteSite(String id) {
		SiteBean delBean = siteDAO.findByID(id);
		siteDAO.remove(delBean);
		return true;
	}

	public List<SiteBean> getAll() {
		return siteDAO.getAll();
	}

	public SiteBean getSiteByID(String id) {
		return siteDAO.findByID(id);
	}

	public SiteBean updateSite(String id, MonitorTypeBean monitorType, AreaBean area,
			String name, String gprsID, ValleyBean valley, String qualityName, int qualityLevel,
			String contact, String telephone, String mobile, String eMail, String ipAddress,
			String port, int freqMin, String gpsFixed, String gpsLongitude, String gpsLatitude,
			String remark, float longitudeSetoff, float latitudeSetoff) {
		SiteBean newBean = siteDAO.findByID(id);
		newBean.setMonitorType(monitorType);
		newBean.setArea(area);
		newBean.setName(name);
		newBean.setGprsID(gprsID);
		newBean.setValley(valley);
		newBean.setQualityName(qualityName);
		newBean.setQualityLevel(qualityLevel);
		newBean.setContact(contact);
		newBean.setTelephone(telephone);
		newBean.setMobile(mobile);
		newBean.seteMail(eMail);
		newBean.setIpAddress(ipAddress);
		newBean.setPort(port);
		newBean.setFreqMin(freqMin);
		newBean.setGpsFixed(gpsFixed);
		newBean.setFixtedLongitude(gpsLongitude);
		newBean.setFixtedLatitude(gpsLatitude);
		newBean.setRemark(remark);
		newBean.setLongitudeSetoff(longitudeSetoff);
		newBean.setLatitudeSetoff(latitudeSetoff);
		
		siteDAO.update(newBean);
		return newBean;
	}

	public List<SiteBean> getAllOrderByProvince() {
		return siteDAO.getAllOrderByProvince();
	}
}
